#include "rootmenu.h"
#include "ui_rootmenu.h"
#include "addstu.h"
#include "rootmodify.h"

#include <QDebug>
#include <QMessageBox>
#include <QTableView>
#include <QCursor>

RootMenu::RootMenu(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::RootMenu)
{
    ui->setupUi(this);
    this->model = new QSqlQueryModel;
    displayall();
}

RootMenu::~RootMenu()
{
    delete ui;
}

QString RootMenu::selectstr(int index, QString cnt)   //返回SQL条件查询语句
{
    QString column;
    switch(index)
    {
        case 1:
        column = "s.Name";
        break;
        case 2:
        column = "s.UID";
        break;
        case 3:
        column = "s.Sex";
        break;
        case 4:
        column = "p.Policy";
        break;
        case 5:
        column = "a.Academy";
        break;
        case 6:
        column = "m.Major";
        break;
        case 7:
        column = "Grade";
        break;
        case 8:
        column = "Class";
        break;
        case 9:
        column = "Phone";
        break;
    }
    QString sqlstr = defaultstr + QString(" WHERE %1 = '%2'").arg(column).arg(cnt);
    return sqlstr;
}

int RootMenu::searchresult(QString sqlstr)  //返回SQL查询的结果数
{
    QSqlQuery test;
    test.exec(sqlstr);
    int i = 0;
    while(test.next())
        i++;
    return i;
}

void RootMenu::displayall(QString sqlstr)
{
    this->model->clear();

    //qDebug() << sqlstr;

    //设置表头
    //************************************************属性名？？？？？*************************************
    this->model->setHeaderData(0, Qt::Horizontal, tr("姓名"));
    this->model->setHeaderData(1, Qt::Horizontal, tr("学号"));
    this->model->setHeaderData(2, Qt::Horizontal, tr("性别"));
    this->model->setHeaderData(3, Qt::Horizontal, tr("政治面貌"));
    this->model->setHeaderData(4, Qt::Horizontal, tr("学院"));
    this->model->setHeaderData(5, Qt::Horizontal, tr("专业"));
    this->model->setHeaderData(6, Qt::Horizontal, tr("年级"));
    this->model->setHeaderData(7, Qt::Horizontal, tr("班级"));
    this->model->setHeaderData(8, Qt::Horizontal, tr("电话"));

    //设置每一列的宽度
    this->ui->tableView->setColumnWidth(0, 10);
    this->ui->tableView->setColumnWidth(0, 70);
    this->ui->tableView->setColumnWidth(0, 5);
    this->ui->tableView->setColumnWidth(0, 30);
    this->ui->tableView->setColumnWidth(0, 130);
    this->ui->tableView->setColumnWidth(0, 130);
    this->ui->tableView->setColumnWidth(0, 5);
    this->ui->tableView->setColumnWidth(0, 5);
    this->ui->tableView->setColumnWidth(0, 100);

    setCursor(Qt::BusyCursor);
    model->setQuery(sqlstr);
    this->ui->tableView->setModel(model);
    this->ui->tableView->show();
    setCursor(Qt::ArrowCursor);
}

void RootMenu::on_root_add_clicked()
{
    //调用添加学生窗口
    setCursor(Qt::BusyCursor);
    addStu *aS;
    aS = new addStu();
    aS->menu = this;
    aS->setdbconn(dbconn);
    setCursor(Qt::ArrowCursor);
    aS->show();
    //displayall();
}

void RootMenu::on_root_query_clicked()
{
    //获取用户输入
    int index = this->ui->root_query_way->currentIndex();
    QString cnt = this->ui->root_query_input->text();

    //显示查询到的内容,如果没有查询到则显示全部内容
    if(cnt.length() == 0){
        QMessageBox::critical(this, "ERROR", "请输入查找对象", "确定");
        displayall();
    }
    else if(index == 0){
        QMessageBox::critical(this, "ERROR", "请输入查找方式", "确定");
        displayall();
    }
    else{
        setCursor(Qt::BusyCursor);
        QString sqlstr = selectstr(index, cnt);
        if(!searchresult(sqlstr)){
            setCursor(Qt::ArrowCursor);
            QMessageBox::critical(this, "FAILED", "未找到此学生", "确定");
            displayall();
        }
        else{
            setCursor(Qt::ArrowCursor);
            displayall(sqlstr);
        }
    }
}

void RootMenu::on_root_delete_clicked()
{
    int index = this->ui->root_query_way->currentIndex();
    QString cnt = this->ui->root_query_input->text();

    if(cnt.length() == 0){
        QMessageBox::critical(this, "ERROR", "请输入删除对象", "确定");
    }
    else if(index == 0){
        QMessageBox::critical(this, "ERROR", "请输入查找方式", "确定");
    }
    else{
        setCursor(Qt::BusyCursor);
        QString sqlstr = selectstr(index, cnt);
        int i = searchresult(sqlstr);
        if(i == 1){ //结果集唯一
            setCursor(Qt::ArrowCursor);
            int ret = QMessageBox::warning(this, "WARNING", "确定删除该学生么", "确定", "取消");
            if(!ret){
                setCursor(Qt::BusyCursor);
                QSqlQuery rm;
                rm.exec(sqlstr);
                rm.next();
                QString UID = rm.value("UID").toString();   //获得删除目标的UID
                rm.exec(QString("DELETE FROM users WHERE Account = '%1'").arg(UID));
                rm.exec(QString("DELETE FROM students WHERE UID = '%1'").arg(UID));
                setCursor(Qt::ArrowCursor);
                QMessageBox::information(this, "SUCCESS", "删除成功", "确定");
            }
        }
        else if(!i){
            setCursor(Qt::ArrowCursor);
            QMessageBox::critical(this, "FAILED"," 未找到该学生", "确定");
        }
        else{
            setCursor(Qt::ArrowCursor);
            QMessageBox::critical(this, "ERROR", "请输入能准确定位该学生的信息", "确定");
        }
    }
    displayall();
}

void RootMenu::on_root_modify_clicked()
{
    int index = this->ui->root_query_way->currentIndex();
    QString cnt = this->ui->root_query_input->text();

    if(cnt.length() == 0){
        QMessageBox::critical(this, "ERROR", "请输入修改对象", "确定");
    }
    else if(index == 0){
        QMessageBox::critical(this, "ERROR", "请输入查找方式", "确定");
    }
    else{
        setCursor(Qt::BusyCursor);
        QString sqlstr = selectstr(index, cnt);
        int i = searchresult(sqlstr);
        if(i == 1){
            QSqlQuery SUID;
            SUID.exec(sqlstr);
            SUID.next();
            QString UID = SUID.value("UID").toString();
            rootModify * rM;
            rM = new rootModify();
            rM->menu = this;
            rM->UID = UID;
            //qDebug() << rM->UID;
            rM->setdbconn(dbconn);
            rM->showui();
            setCursor(Qt::ArrowCursor);
            rM->exec();
        }
        else if(!i){
            setCursor(Qt::ArrowCursor);
            QMessageBox::critical(this, "ERROR"," 未找到该学生", "确定");
        }
        else{
            setCursor(Qt::ArrowCursor);
            QMessageBox::critical(this, "ERROR", "请输入能准确定位该学生的信息", "确定");
        }
    }
    //displayall();
}
